#include <iostream>
#include <pthread.h>
#include <unistd.h>


pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int cnt = 0;

void* Count(void* arg)
{
    uint64_t id = (uint64_t)arg;
    pthread_detach(pthread_self());
    std::cout << "pthread: " << id << " , creat success" << std::endl;
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        std::cout << "pthread: " << id << " , cnt: " << cnt++ << std::endl;
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{
    for(uint64_t i = 0; i < 5; ++i)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, Count, (void*)i);
        usleep(1000);
    }

    sleep(3);
    std::cout << "main thread ctrl begin: " << std::endl;
    while(true)
    {
        sleep(1);
        pthread_cond_signal(&cond);
        std::cout << "signal one thread..." << std::endl;
    }

    pthread_cond_destroy(&cond);
    std::cout << "main thread exit" << std::endl;
    return 0;
}